package com.sino.struts2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.googlecode.jsonplugin.annotations.SMDMethod;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.IBasicInfoService;
import com.sino.business.IPatentManageQxkwService;
import com.sino.business.IPatentManageService;
import com.sino.business.IStaticDataService;
import com.sino.business.service.LoginInfo;
import com.sino.model.BasicInfoModel;
import com.sino.model.QxkwModel;
import com.sino.util.PageControl;
import com.sino.util.SysParameter;

import com.opensymphony.xwork2.Action;

public class UserAction extends ActionSupport {
	private IStaticDataService staticDateService;// 基础数据

	private IBasicInfoService basicInfoService;

	private IPatentManageQxkwService patentManageQxkw;

	private List qxkwList;// 推荐单位

	private List<LoginInfo> userList;// 用户列表

	private LoginInfo logininfo;// 登陆用户信息

	private IPatentManageService ipatentManageService;// 专利管理

	private String page;// 当前页

	private List<BasicInfoModel> basicInfoList;

	private String pageStr;

	private Integer countTbl1;

	private String userid;

	private String qxkwStr;// 区县科委链接字符串

	private String qxkz = "0000";

	private String qxkwTitle = "";

	private int pageRow;

	private List qxkwTypeList;// 推荐单位类型

	public String getQxkz() {
		return qxkz;
	}

	public void setQxkz(String qxkz) {
		this.qxkz = qxkz;
	}

	public String getQxkwStr() {
		return qxkwStr;
	}

	public void setQxkwStr(String qxkwStr) {
		this.qxkwStr = qxkwStr;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public List getQxkwList() {
		return qxkwList;
	}

	public void setQxkwList(List qxkwList) {
		this.qxkwList = qxkwList;
	}

	public String verfiyUser() {

		return Action.SUCCESS;
	}

	@SMDMethod
	public LoginInfo doSomething(LoginInfo loginInfo) {
		boolean flag = this.getIpatentManageService().verifyUser(
				loginInfo.getName().trim());
		if (flag == false) {
			loginInfo.setName("");
		} else {
			loginInfo.setName("user");

		}
		return loginInfo;
	}

	@SMDMethod
	public LoginInfo verifyPassword(LoginInfo loginInfo) {

		boolean flag = this.getIpatentManageService().verifyPassword(
				loginInfo.getName(), loginInfo.getPassword().trim());

		if (flag == false) {
			loginInfo.setName("");
		} else {
			loginInfo.setName("user");

		}
		return loginInfo;

	}

	public String viewBasicInfo() {

		this.setQxkwList(this.getStaticDateService().findQxkwList());
		return this.SUCCESS;
	}

	/**
	 * 修改登陆用户自己
	 * 
	 * @return
	 */
	public String viewUserInfo() {
		Map session = ActionContext.getContext().getSession();
		LoginInfo loginInfo = (LoginInfo) session.get("logininfo");
		logininfo = new LoginInfo();
		this.setQxkwList(this.getStaticDateService().findQxkwList());
		this.getLogininfo().setQxkw(loginInfo.getQxkw());
		this.logininfo.setQxkw(loginInfo.getQxkw());
		return this.SUCCESS;
	}

	public String listUsers() {

		Map session = ActionContext.getContext().getSession();
		LoginInfo loginInfo = (LoginInfo) session.get("logininfo");
		logininfo = new LoginInfo();

		this.setQxkwList(this.getStaticDateService().findQxkwList());

		this.getLogininfo().setQxkw(loginInfo.getQxkw());
		userList = this.getIpatentManageService().getUser(loginInfo.getQxkw());

		this.setUserList(userList);
		return SUCCESS;
	}

	public String getBasicInfoPageList() {
		countTbl1 = this.basicInfoService.findFenShu1();
//		ServletActionContext.getRequest().setAttribute("countTbl1", countTbl1);
//		System.out.println(countTbl1);
		String isqualified = " (basicinfo.isqualified=0 or basicinfo.isqualified is null )and basicinfo.year="
				+ SysParameter.CURRENT_YEAR + " ";// 没用通过合格
		this.setQxkwStr(this.ipatentManageService.getQxkwLink(this
				.getStaticDateService().findQxkwList()));// 获得区县字符串链接表
		Map session = ActionContext.getContext().getSession();
		LoginInfo logininfo = (LoginInfo) session.get("logininfo");
		String qxkw = logininfo.getQxkw();
		PageControl pageControl = new PageControl();// 翻页控制类
		// CreatQueryCondition creatQueryCondition = new
		// CreatQueryCondition();// 条件查询类
		pageControl.setPage(page);// 设置当前页
		pageControl.setPageSize(20);// 设置每页显示的记录数量
		pageControl.setRowCount(this.getIpatentManageService()
				.getBasicInfoPageCount(isqualified, qxkw));// 设置总记录数
		int startRow = (pageControl.getPage() - 1) * pageControl.getPageSize();// 开始行数
		int pageSize = pageControl.getPageSize();// 每页显示的数量
		pageStr = pageControl.getPageInfo("basicInfoPageList.action?qxkw="
				+ qxkw, "");

		basicInfoList = this.getIpatentManageService().getBasicInfoPageList(
				startRow, pageSize, isqualified, qxkw);// 分页查询考生信息
		qxkwTitle = SysParameter.CURRENT_YEAR + "年北京市发明专利基本信息列表";
		this.setPageRow(startRow);

		/*
		 * Iterator<BasicInfoModel> iterator=basicInfoList.iterator();
		 * while(iterator.hasNext()){ BasicInfoModel bim=iterator.next();
		 * System.out.println(bim.getName()+"--"+bim.getHy()); }
		 */
		this.setBasicInfoList(basicInfoList);
		this.setPageStr(pageStr);
		if (qxkw.equals("0000")) {

			return "bigsuccess";
		} else {

			return "success";
		}
	}

	public String getYesBasicInfoPageList() {
		String isqualified = " isqualified=1  and  basicinfo.year="
				+ SysParameter.CURRENT_YEAR + " ";// 没用通过合格
		this.setQxkwStr(this.ipatentManageService.getQxkwLink(this
				.getStaticDateService().findQxkwList()));// 获得区县字符串链接表
		Map session = ActionContext.getContext().getSession();
		LoginInfo logininfo = (LoginInfo) session.get("logininfo");
		String qxkw = logininfo.getQxkw();
		PageControl pageControl = new PageControl();// 翻页控制类
		// CreatQueryCondition creatQueryCondition = new
		// CreatQueryCondition();// 条件查询类
		pageControl.setPage(page);// 设置当前页
		pageControl.setPageSize(20);// 设置每页显示的记录数量
		pageControl.setRowCount(this.getIpatentManageService()
				.getBasicInfoPageCount(isqualified, qxkw));// 设置总记录数
		int startRow = (pageControl.getPage() - 1) * pageControl.getPageSize();// 开始行数
		int pageSize = pageControl.getPageSize();// 每页显示的数量
		pageStr = pageControl.getPageInfo("yesbasicInfoPageList.action?qxkw="
				+ qxkw, "");

		basicInfoList = this.getIpatentManageService().getBasicInfoPageList(
				startRow, pageSize, isqualified, qxkw);// 分页查询考生信息
		qxkwTitle = SysParameter.CURRENT_YEAR + "年北京市发明专利基本信息列表";
		this.setPageRow(startRow);

		/*
		 * Iterator<BasicInfoModel> iterator=basicInfoList.iterator();
		 * while(iterator.hasNext()){ BasicInfoModel bim=iterator.next();
		 * System.out.println(bim.getName()+"--"+bim.getHy()); }
		 */
		this.setBasicInfoList(basicInfoList);
		this.setPageStr(pageStr);
		if (qxkw.equals("0000")) {

			return "bigsuccess";
		} else {

			return "success";
		}
	}

	public List getQxkwByCutId(List qxkwList) {
		List qmList = new ArrayList();
		Iterator it = qxkwList.iterator();
		while (it.hasNext()) {
			QxkwModel qm = (QxkwModel) it.next();

			qm.setId(qm.getId().substring(0, 2));
			// System.out.println(qm.getId());
			qm.setName(qm.getName());
			qmList.add(qm);
		}
		return qmList;
	}

	public String saveUser() {
		this.getIpatentManageService().saveUser(logininfo);
		return SUCCESS;
	}

	public String changePassword() {
		Map session = ActionContext.getContext().getSession();
		LoginInfo loginInfo = (LoginInfo) session.get("logininfo");
		// System.out.println(loginInfo.getLxdh());
		this.getIpatentManageService().updatePassword(logininfo,
				loginInfo.getId());
		return SUCCESS;
	}

	public String modifyUserDo() {
		Map session = ActionContext.getContext().getSession();
		LoginInfo loginInfo = (LoginInfo) session.get("logininfo");

		this.getIpatentManageService().updateUser(logininfo, loginInfo.getId());
		return SUCCESS;
	}

	public String delUser() {

		this.ipatentManageService.deleteUser(userid);

		return SUCCESS;
	}

	/*
	 * 修改用户
	 */
	public String viewUsers() {

		this.setQxkwList(this.getStaticDateService().findQxkwList());

		this.setLogininfo(this.ipatentManageService.getLoginInfo(userid));
		return SUCCESS;
	}

	public String modifyUsers() {
		this.ipatentManageService.updateUsers(logininfo);
		return SUCCESS;
	}

	public IStaticDataService getStaticDateService() {
		return staticDateService;
	}

	public void setStaticDateService(IStaticDataService staticDateService) {
		this.staticDateService = staticDateService;
	}

	public IPatentManageService getIpatentManageService() {
		return ipatentManageService;
	}

	public void setIpatentManageService(
			IPatentManageService ipatentManageService) {
		this.ipatentManageService = ipatentManageService;
	}

	public LoginInfo getLogininfo() {
		return logininfo;
	}

	public void setLogininfo(LoginInfo logininfo) {
		this.logininfo = logininfo;
	}

	public List<LoginInfo> getUserList() {
		return userList;
	}

	public void setUserList(List<LoginInfo> userList) {
		this.userList = userList;
	}

	public IBasicInfoService getBasicInfoService() {
		return basicInfoService;
	}

	public void setBasicInfoService(IBasicInfoService basicInfoService) {
		this.basicInfoService = basicInfoService;
	}

	public List<BasicInfoModel> getBasicInfoList() {
		return basicInfoList;
	}

	public void setBasicInfoList(List<BasicInfoModel> basicInfoList) {
		this.basicInfoList = basicInfoList;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}

	public String getQxkwTitle() {
		return qxkwTitle;
	}

	public void setQxkwTitle(String qxkwTitle) {
		this.qxkwTitle = qxkwTitle;
	}

	public int getPageRow() {
		return pageRow;
	}

	public void setPageRow(int pateRow) {
		this.pageRow = pateRow;
	}

	public IPatentManageQxkwService getPatentManageQxkw() {
		return patentManageQxkw;
	}

	public void setPatentManageQxkw(IPatentManageQxkwService patentManageQxkw) {
		this.patentManageQxkw = patentManageQxkw;
	}

	public List getQxkwTypeList() {
		return qxkwTypeList;
	}

	public void setQxkwTypeList(List qxkwTypeList) {
		this.qxkwTypeList = qxkwTypeList;
	}

	public Integer getCountTbl1() {
		return countTbl1;
	}

	public void setCountTbl1(Integer countTbl1) {
		this.countTbl1 = countTbl1;
	}

}
